package com.alisls.demo.cas.ics.filter;

import com.alisls.demo.cas.ics.util.CASUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 设置本地用户信息过滤器
 *
 * @author Ke Wang
 */
@Slf4j
public class LocalUserInfoFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String loginName = CASUtil.getAccountNameFromCas(request);
        if (StringUtils.hasLength(loginName)) {
            log.info("访问者：{}", loginName);
            request.getSession().setAttribute("loginName", loginName);
        }

        chain.doFilter(servletRequest, servletResponse);
    }
}
